package lk.icta.common.dao.impl;

import lk.icta.common.AppConstants;
import lk.icta.common.ErrorCodes;
import lk.icta.common.dao.PreloadableDAO;
import lk.icta.common.dao.ProvinceDAO;
import lk.icta.common.entity.District;
import lk.icta.common.entity.Province;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.persistence.Query;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Duminda Dharmakeerthi
 * @author Supun Viraj Rathnayaka
 */
public class ProvinceDAOImpl extends BaseDAO implements ProvinceDAO, PreloadableDAO {

     private final Map<Integer, String> provinceList = new HashMap<Integer, String>();

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public void addProvince(Province province) {
        logger.debug("Adding a Province [Id: {}\tName: {}]", province.getId(), province.getEnProvinceName());
        em.persist(province);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public Province getProvinceById(int provinceId) {
        logger.debug("Loading province {}", provinceId);
        return em.find(Province.class, provinceId);
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public void updateProvince(Province province) {
        logger.debug("Updating a Province [Id: {}\tName: {}]", province.getId(), province.getEnProvinceName());
        em.merge(province);
    }

    @Transactional(propagation = Propagation.SUPPORTS)
    public List<Province> getAllProvinceNames() {
        Query q = em.createNamedQuery("findAllProvinces");
        return q.getResultList();
    }

    public Map<Integer,String> getProvinceList(String language){
        logger.debug("getting province list :{}",provinceList.size());
        if (AppConstants.SINHALA.equals(language)) {
            for (int i = 1; i < (AppConstants.NO_OF_PROVINCE + 1); i++) {
               provinceList.put(i,this.getProvinceById(i).getSiProvinceName());
            }
        } else if (AppConstants.ENGLISH.equals(language)) {
            for (int i = 1; i < (AppConstants.NO_OF_PROVINCE + 1); i++) {
                provinceList.put(i,this.getProvinceById(i).getEnProvinceName());
            }
        } else if (AppConstants.TAMIL.equals(language)) {
            for (int i = 1; i < (AppConstants.NO_OF_PROVINCE + 1); i++) {
                provinceList.put(i,this.getProvinceById(i).getTaProvinceName());
            }
        } else {
            handleException("Unsupported language : " + language, ErrorCodes.INVALID_LANGUAGE);
        }

        if (provinceList != null)
            return provinceList;
        else
            return Collections.emptyMap();
    }

    @Transactional(propagation = Propagation.NEVER, readOnly = true)
    public void preload() {
        logger.debug("Call getting province preload");
        Query query = em.createQuery("SELECT p FROM Province p order by p.enProvinceName");
        List<Province> proList = query.getResultList();
        for (Province r : proList) {
            int provinceId = r.getId();
            String name = r.getEnProvinceName();
            provinceList.put(provinceId, name);
             logger.debug("getting province id :{} Name :{}",provinceId,name);
        }
    }
}
